package NetWork.Exercise002;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * ClassName:Client
 * Package:NetWork.Exercise001
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-22:41
 * @Version:v1.0
 */
public class Client {
    /*TCP通信练习
    * 客户端：发送一条数据,接收服务端反馈的消息并打印
    * 服务端：接收数据并打印,再给客户端反馈消息*/
    public static void main(String[] args) throws IOException {
        //创建socket 对象并连接服务端
        Socket socket = new Socket("127.0.0.1", 1000);
        //写出数据
        String str = "见到你很高兴";
        OutputStream os = socket.getOutputStream();
        os.write(str.getBytes());

        //结束标记
        socket.shutdownOutput();

        //接收服务器端回写的数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int b;
        while ((b=br.read())!=-1){
            System.out.print((char) b);
        }

        //释放资源
        os.close();
        socket.close();

    }
}
